﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ZhuXingFamilys.utils
{
    /// <summary>
    /// WaittingDlg.xaml 的交互逻辑
    /// </summary>
    public partial class WaittingDlg : Window
    {
        [DllImport("user32.dll")]
        static extern bool EnableWindow(IntPtr hWnd, bool bEnable);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        //构造函数
        public WaittingDlg(string sWaittingInfo = "", string sWindowColor = "", string sDlgBorderColor = "", int nBorderWidth = 5, int nWidth = 0, int nHeight = 0)
        {
            InitializeComponent();
            if (nWidth != 0)
            {
                this.Width = nWidth;
            }
            if (nHeight != 0)
            {
                this.Height = nHeight;
            }
            this.sWaittingInfo = "请等待。。。";    //提示信息
            this.sWindowColor = "White";                                        //窗口颜色
            this.sDlgBorderColor = "#FF5B9BD1";                                 //边框颜色
            this.nBorderWidth = 5;                                              //边框宽度
            if (sWaittingInfo != "")
            {
                this.sWaittingInfo = sWaittingInfo;
            }
            if (sWindowColor != "")
            {
                this.sWindowColor = sWindowColor;
            }
            if (sDlgBorderColor != "")
            {
                this.sDlgBorderColor = sDlgBorderColor;
            }
            if (nBorderWidth > 0)
            {
                this.nBorderWidth = nBorderWidth;
            }
            this.Closing += new System.ComponentModel.CancelEventHandler(Window1_Closing);
        }
        //关闭事件
        void Window1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Closing -= new System.ComponentModel.CancelEventHandler(Window1_Closing);
            IntPtr handle = (new System.Windows.Interop.WindowInteropHelper(this)).Handle;
            IntPtr ownerhandle = (new System.Windows.Interop.WindowInteropHelper(this.Owner)).Handle;
            EnableWindow(handle, false);
            EnableWindow(ownerhandle, true);
        }
        //初始化完成
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            IntPtr handle = (new System.Windows.Interop.WindowInteropHelper(this)).Handle;
            EnableWindow(handle, true);
            SetForegroundWindow(handle);
            this.AutoChangeSize();
        }
        //等待提示信息
        public string sWaittingInfo
        {
            set
            {
                txtWaittingInfo.Text = value;
            }
            get
            {
                return txtWaittingInfo.Text.ToString().Trim();
            }
        }
        //窗口背景 Img
        public string sWindowBkImg
        {
            set
            {
                Uri uri = new Uri(value, UriKind.RelativeOrAbsolute);
                BitmapImage bimg = new BitmapImage(uri);
                this.Background = new ImageBrush(bimg);
            }
        }
        //窗口背景颜色 String
        public string sWindowColor
        {
            /***********用法*******
             * sWindowColor = "Red";
            *********************/
            set
            {
                this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(value));
            }
        }
        //窗口背景颜色 Color
        public Color cWindowColor
        {
            /***********用法*******
             * cWindowColor = System.Windows.Media.Colors.Red;
            *********************/
            set
            {
                bDlgBorder.BorderBrush = new SolidColorBrush(value);
            }
        }
        //边框颜色 String
        public string sDlgBorderColor
        {
            /***********用法*******
             * sDlgBorderColor = "Red";
            *********************/
            set
            {
                bDlgBorder.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(value));
            }
        }
        //边框颜色 Color
        public Color cDlgBorderColor
        {
            /***********用法*******
             * cDlgBorderColor = System.Windows.Media.Colors.Red;
            *********************/
            set
            {
                bDlgBorder.BorderBrush = new SolidColorBrush(value);
            }
        }
        //边框宽度
        public int nBorderWidth
        {
            set
            {
                bDlgBorder.BorderThickness = new Thickness(value);
            }
        }
        //自动调整文本上下居中
        private void AutoChangeSize()
        {
            if (this.txtWaittingInfo.ActualHeight + 8 > this.ActualHeight)
            {
                this.txtWaittingInfo.SetValue(VerticalAlignmentProperty, VerticalAlignment.Top);
                this.txtWaittingInfo.Margin = new Thickness(0, 2, 0, 0);
            }
            else
            {
                this.txtWaittingInfo.SetValue(VerticalAlignmentProperty, VerticalAlignment.Center);
            }
        }
    }

}
